<?php

namespace backend\controllers;

use app\models\User;
use Yii;
use yii\data\ActiveDataProvider;
use yii\filters\VerbFilter;
use yii\web\NotFoundHttpException;
use function json_encode;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends CommonController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }


    /**
     * 登录
     */
    public function actionLogin()
    {
        //用来保存用户名以及密码的记住信息
        $user = User::find()->where(['username'=>$_POST['user']])->asArray()->one();
        if(!empty($user)){
            if(Yii::$app->getSecurity()->validatePassword($_POST['password'],$user['password_hash'])){
//                $cookies = Yii::$app->response->cookies;
//                $cookies->add(new \yii\web\Cookie([
//                    'name' => 'username',
//                    'value' => $user['username'],
//                    'expire'=>time()+3600*24*30
//                ]));
//                $cookies->add(new \yii\web\Cookie([
//                    'name' => 'password',
//                    'value' => $user['password_hash'],
//                    'expire'=>time()+3600*24*30
//                ]));
//                $cookies = Yii::$app->request->cookies;
                return json_encode($user);
            }
        }else{
            return 0;
        }
    }
    /**
     * 查找用户
     */
    public function actionFind($username)
    {
        //用来保存用户名以及密码的记住信息
        $model = new User();
        $res = $model->find()->select("user.*")->where(['username'=>$username])->asArray()->one();
        if(!empty($res)){
            return json_encode($res);
        }else{
            return 0;
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();
        //清除cookie以防止不同胡浏览器出现兼容问题无法退出的问题
        Yii::$app->response->cookies->remove('_identity-backend');
        return 1;
    }






    /**
     * Lists all User models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => User::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single User model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new User model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new User();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing User model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing User model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the User model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return User the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    /**
     * 坐席的电话号码的列表
     */
    public function actionList()
    {
        return $this->getList(new User(),'','',['like','username',is_set($_POST,'username')]);
    }

}
